package com.test.service.impl;

import java.io.UnsupportedEncodingException;
import java.lang.annotation.Target;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.test.domain.Company;
import com.test.domain.TAccountuser;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.domain.User;
import com.test.mapper.UserMapper;
import com.test.service.UserService;

import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

	@Autowired
	private UserMapper userMapper;
	
	@Override
	protected Mapper<User> getMapper() {
		return this.userMapper;
	}

	@Override
	public User findUserByUserName(String userName) {
		return this.userMapper.getUserByUserName(userName);
	}

	@Override
	public Boolean updatePassword(int id, String newPassword) {
		return this.userMapper.updatePassword(id,newPassword);
	}

	@Override
	public Boolean insterInformation(@Param("name") String name,@Param("gender") String gender,@Param("cardid") String cardid,@Param("mobile") String mobile,@Param("resume") String resume,@Param("companyid") String companyid) {
		System.out.println(name);
		 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String createtime=sdf.format(new Date());
		return this.userMapper.insterInformation(name,gender,cardid,mobile,resume,companyid,createtime);
	}

	@Override
	public List<User> getUserCompany() {
		// TODO Auto-generated method stub
		return this.userMapper.getUserCompany();
	}

	@Override
	public void deleteBatchByIds(String[] idsStr) {
		this.userMapper.deleteBatchByIds(idsStr);
	}

	@Override
	public PageInfo<User> getListByPage(int currentNum, int pageSize,String name) {
		
		Example cond = new Example(User.class);
        if (!StringUtils.isEmpty(name)) {
        	try {
				name = new String(name.getBytes("utf-8"),"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			cond.createCriteria().andLike("userName","%" + name + "%");
        }

		PageHelper.startPage(currentNum, pageSize);
		List<User> list = this.userMapper.selectByExample(cond);

		return new PageInfo<User>(list);
	}

	@Override
	public TAccountuser getInformById(int id) {

		TAccountuser auser=this.userMapper.getInformById(id);
		return auser;
	}


	@Override
	public PageInfo<TAccountuser> getCompanyInformListByPage(int currentNum, int pageSize, String name,String companyid) {
		Example cond = new Example(User.class);
		if (!StringUtils.isEmpty(name)) {
			try {
				name = new String(name.getBytes("iso8859-1"),"utf-8");
				System.out.println(name);
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			cond.createCriteria().andLike("userName",name);
		}

		PageHelper.startPage(currentNum, pageSize);
		//List<User> list = this.userMapper.selectByExample(cond);
		List<TAccountuser> list=this.userMapper.getCompanyInform(companyid);

		for(TAccountuser u:list)
		{
			//System.out.println(u.getUserName()+u.getCompanyName());
		}
		return new PageInfo<TAccountuser>(list);
	}
	@Override
	public void saveUserRole(int userId, String roleIdsStr) {
		// 解绑
		this.userMapper.deleteUserRoleByUserId(userId);
		
		if (StringUtils.isNotEmpty(roleIdsStr)) {
			// 绑定
			String[] roleIds = roleIdsStr.split(",");
			List<Map<String,Integer>> params = new ArrayList<>(roleIds.length);
	        Map<String,Integer> param = null;
	        for (String roleId : roleIds) {
	            param = new HashMap<>(2);
	            param.put("userId",userId);
	            param.put("roleId",Integer.parseInt(roleId));
	            params.add(param);
	        }
	        
	        this.userMapper.saveUserRole(params);
		}
	}


}
